require( '$d/css/common.less' );
require( './style.less' );
define( [
  'jquery',
  'require',
  'header',
  'message',
  'api',
  'bigwe_utils',
  'pagination',
  'nav'
], function( $, require ) {
  //引入插件
  var thisApi = require( 'api' );
  var msg = require( 'message' );
  var flag = 0;

  //时间选择下拉菜单
  $( "#select_time" ).selectpicker( {
    size: 8
  } ).change( function() {
    scoreList.options.time_type = $( "#select_time" ).val();
    scoreList.className = classNameDic[ flag ].name;
    for ( var i in classNameDic ) {
      classNameDic[ i ].click = true;
      classNameDic[ i ].isOK = true;
    }
    for ( var i in optionsDic ) {
      optionsDic[ i ].p = 1;
    }
    scoreList.getScore( scoreList.className, classNameDic[ flag ].id );
  } );

  //传给后台的数据结构字典
  var optionsDic = {
    "0": {
      time_type: "week",
      score_type: "",
      l: 20,
      p: 1
    },
    "1": {
      time_type: "week",
      score_type: "income",
      l: 20,
      p: 1
    },
    "2": {
      time_type: "week",
      score_type: "pay",
      l: 20,
      p: 1
    }
  };

  //功能模块
  var scoreList = {
    options: {
      time_type: "week",
      score_type: "",
      l: 20,
      p: 1
    },
    className: {
      name: "js_scoreall tbody"
    },
    //创建html
    getScoreHtml: function( json ) {
      var scoreHtml = $( $( "#score_item" ).html() );

      $( ".score_table_time", scoreHtml ).text( json.time || '' );
      if ( json.integral > 0 ) {
        $( ".score_table_balance", scoreHtml ).addClass( "green" ).text( json.integral );
      } else {
        $( ".score_table_balance", scoreHtml ).addClass( 'red' ).text( json.integral );
      }
      $( '.score_table_remark', scoreHtml ).text( json.instruction || '' );

      return scoreHtml;
    },
    //获取积分数量用于展示
    getScoreCount: function() {
      $.ajax( {
        type: "get",
        url: thisApi.SCORE.GET_SCORE_COUNT,
        success: function( data ) {
          var tmp = JSON.parse( data );
          var scoreCount = tmp.data;
          $( "#all_score" ).text( scoreCount.total );
          $( "#now_score" ).text( scoreCount.now );
          $( "#pay_score" ).text( scoreCount.pay );
        }
      } )
    },
    //从后台获取数据并添加到界面
    getScore: function( className, pagingId ) {
      msg.show( "正在加载" );
      $.ajax( {
        type: "post",
        data: this.options,
        url: thisApi.SCORE.GET_SCORE_LIST,
        success: function( data ) {

          //处理后台返回数据
          var totalScore = JSON.parse( data );

          //判断后台是否处理成功
          if ( totalScore.ret != 0 ) {
            alert( totalScore.msg );
            return;
          }
          msg.hide();

          //返回的数据数量
          var totle = totalScore.data.totle;

          $( "." + className ).empty();

          for ( var i in totalScore.data.list ) {
            $( "." + className ).append( scoreList.getScoreHtml( totalScore.data.list[ i ] ) );
          }

          if ( totle <= scoreList.options.l ) {
            classNameDic[ flag ].isOK = true;
            $( '#' + pagingId ).hide();
          }

          if ( totle > scoreList.options.l && classNameDic[ flag ].isOK == true ) {
            classNameDic[ flag ].func( totle );
          }
        },
        error: function( data ) {
          var error = JSON.parse( data );
          alert( error.msg );
        }
      } )
    }
  };

  scoreList.getScoreCount();
  scoreList.getScore( "js_scoreall tbody", "score_all" );

  //栏目切换
  $( '.bw-columns' ).bwColumns( [ '全部积分', '收入明细', '支出明细' ], function( index, prev ) {
    var idx = index.index();
    flag = idx;
    scoreList.options = optionsDic[ idx ];
    if ( !!classNameDic[ idx ].click ) {
      scoreList.options.time_type = $( "#select_time" ).val();
      scoreList.options.p = 1;
      scoreList.className = classNameDic[ idx ].name;
      console.log( scoreList.className );
      scoreList.getScore( scoreList.className, classNameDic[ flag ].id );
      classNameDic[ idx ].click = !classNameDic[ idx ].click;
    }
  }, '.socre_warp' );
  $( '.score_cont:first' ).show();

  //    分页效果
  var pagingAll = function( _parameter ) {
    $( "#score_all" ).pagination( _parameter, {
      num_edge_entries: 1,
      num_display_entries: 8,
      items_per_page: 20,
      link_to: 'javascript:window.scrollTo(0,0);',
      prev_text: '上一页',
      next_text: '下一页',
      callback: function( _index, jq ) {
        if ( classNameDic[ 0 ].isOK == true ) {
          classNameDic[ 0 ].isOK = false;
          return;
        } else {
          scoreList.options.p = _index + 1;
          scoreList.getScore( "js_scoreall tbody", "score_all" );
        }
      }
    } );
    $( '#score_all' ).show();
  };

  var pagingIncome = function( _parameter ) {
    $( "#score_income" ).pagination( _parameter, {
      num_edge_entries: 1,
      num_display_entries: 8,
      items_per_page: 20,
      link_to: 'javascript:window.scrollTo(0,0);',
      prev_text: '上一页',
      next_text: '下一页',
      callback: function( _index, jq ) {
        if ( classNameDic[ 1 ].isOK == true ) {
          classNameDic[ 1 ].isOK = false;
          return;
        } else {
          scoreList.options.p = _index + 1;
          scoreList.getScore( "js_scoreincome tbody", "score_income" );
        }
      }
    } );
    $( '#score_income' ).show();
  };

  var pagingExpend = function( _parameter ) {
    $( "#score_expend" ).pagination( _parameter, {
      num_edge_entries: 1,
      num_display_entries: 8,
      items_per_page: 20,
      link_to: 'javascript:window.scrollTo(0,0);',
      prev_text: '上一页',
      next_text: '下一页',
      callback: function( _index, jq ) {
        if ( classNameDic[ 2 ].isOK == true ) {
          classNameDic[ 2 ].isOK = false;
          return;
        } else {
          scoreList.options.p = _index + 1;
          scoreList.getScore( "js_scoreexpend tbody", "score_expend" );
        }
      }
    } );
    $( '#score_expend' ).show();
  };

  var classNameDic = {
    "0": {
      name: "js_scoreall tbody",
      id: "score_all",
      click: true,
      isOK: true,
      func: pagingAll
    },
    "1": {
      name: "js_scoreincome tbody",
      id: "score_income",
      click: true,
      isOK: true,
      func: pagingIncome
    },
    "2": {
      name: "js_scoreexpend tbody",
      id: "score_expend",
      click: true,
      isOK: true,
      func: pagingExpend
    }
  };
} );
